import request, { baseURL } from '@/utils/request'
import axios from 'axios'

// 用户登录API
export const loginAPI = data => {
  return request({ url: '/v1_0/authorizations', method: 'POST', data })
}

// 发送短信验证码API
export const sendCodeAPI = mobile => {
  return request({ url: `/v1_0/sms/codes/${mobile}` })
}

// 关注&取消关注
export const followAuthorAPI = (id, isFollow) => {
  if (isFollow) {
    // 关注
    return request({
      url: '/v1_0/user/followings',
      method: 'POST',
      data: {
        target: id
      }
    })
  } else {
    // 取关
    return request({
      url: `/v1_0/user/followings/${id}`,
      method: 'DELETE'
    })
  }
}

// 获取当前用户的信息（资料和统计）
export const getUserInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 获取当前用户的个人资料
export const getUserProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 修改头像
export const changePhotoAPI = formData => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formData
  })
}

// 修改个人资料
export const updateUserAPI = user => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: user
  })
}

// 刷新用户token
export const refreshTokenAPI = refreshToken => {
  return axios({
    url: baseURL + '/v1_0/authorizations',
    method: 'PUT',
    header: { Authorization: `Bearer ${refreshToken}` }
  })
}
